﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using AllInOnePluginBL.Helpers;
using NLog;

namespace AllInOnePluginBL.FileHostingExtractors
{
    /// <summary>
    /// Summary description for MegaUploadExtractor
    /// </summary>
    public class MegaUploadExtractor : IFileHostingExtractor
    {
        private readonly Logger _log = LogManager.GetLogger("MegaUploadExtractor");
        private const string VIDEO_URL = "http://www.megaupload.com/?d={0}";
        //private const string URL_REGEX = @"megavideo\.com.*?[v,d][=,/](.{8})";
        private const string MEGAUPLOAD_URL_REGEX = @"megaupload\.com.*?d[=,/](.{8})";

        public static string Referer
        {
            get
            {
                return "http://www.megaupload.com/";
            }
        }

        public List<string> GetMovieUrls(string content)
        {
            var movieCodes = new List<string>();
            var exp = new Regex(MEGAUPLOAD_URL_REGEX, RegexOptions.IgnoreCase);
            var matches = exp.Matches(content);
            foreach (Match match in matches)
            {
                if (match.Success)
                {
                    var movieCode = string.Format(VIDEO_URL, match.Groups[1].Value);
                    if (!movieCodes.Contains(movieCode))
                    {
                        movieCodes.Add(movieCode);
                    }
                }
            }
            return movieCodes;
        }

        public VideoResult ExtractMovieUrl(string movieUrl)
        {
            const string MOVIE_REGEX = "<div class=\"download_member_bl\".*?>.*?<a href=\"http://(.+?)\"";
            string content = null;
            var isStream = false;
            var extractedMovieUrl = string.Empty;

            var premiumCode = Utils.GetMegaVideoPremium();
            if (!string.IsNullOrEmpty(premiumCode))
            {
                content = Utils.GetMegaUploadMovieUrlContent(
                    movieUrl,
                    Encoding.UTF8,
                    string.Format("user={0}", premiumCode),
                    out isStream);
                if (isStream) extractedMovieUrl = content;
            }
            
            if (string.IsNullOrEmpty(content))
            {
                content = Utils.GetPage(movieUrl, Encoding.UTF8);
            }

            if (string.IsNullOrEmpty(content))
            {
                throw new Exception("Cant load url Content");    
            }

            if (!isStream)
            {
                if (content.Contains("class=\"bott_p_na_lnk\""))
                {
                    return new VideoResult(movieUrl, false);
                }

                var match = Regex.Match(content, MOVIE_REGEX, RegexOptions.Singleline);
                if (!match.Success || !match.Groups[1].Value.Contains("megaupload"))
                {
                    const string REGULAR_USER_URL =
                        "<a class=\"download_regular_disabled\"  id=\"dlbuttondisabled\"></a>.*?<a href=\"(.+?)\"";

                    match = Regex.Match(content, REGULAR_USER_URL, RegexOptions.Singleline);
                    extractedMovieUrl = match.Groups[1].Value;
                }
                else
                {
                    var movieFullUrl = match.Groups[1].Value;
                    if (!movieFullUrl.Contains("http"))
                        movieFullUrl = "http://" + movieFullUrl;
                    extractedMovieUrl = movieFullUrl;
                }
                if (!match.Success) return new VideoResult(movieUrl, false);
            }
            
            _log.Debug("MegaUploadExtractor::ExtractMovieUrl: {0}", extractedMovieUrl);

            if (extractedMovieUrl.Contains(".rar") || extractedMovieUrl.Contains(".zip"))
                return new VideoResult(movieUrl, false);

            return new VideoResult(extractedMovieUrl, true);
        }

        public bool IsContainVideoLink(string url)
        {
            return url.Contains("http://www.multiupload.com/MU_") || url.Contains("megaupload");
        }
    }
}